
const db = require('../db/index')

exports.getArtcate = (req, res) => {
    const sql = `select * from ev_artide_cate where is_delete =0 order by id asc`
    db.query(sql, (err, result) => {
        if (err) return res.cc(err)
        res.send({
            status: 0,
            message: '获取文章分类成功',
            data: result
        })
    })
}

//新增文章的接口模块
exports.addArtcleCates = (req, res) => {
    //查询name 或者alias 是否被占用
    const sql = 'select * from ev_artide_cate where name =? or alias=?'
    db.query(sql, [req.body.name, req.body.alias], (err, result) => {
        if (err) return res.cc(err)
        //判断查询回来的length 
        if (result.length === 2) return res.cc('分类名称与分类别名被占用,请更换!')
        if (result.length === 1 && req.body.name === result[0].name && req.body.alias === result[0].alias) {
            return res.cc('分类名称与分类别名被占用,请更换!')
        }
        else if (result.length === 1 && req.body.name === result[0].name) {
            return res.cc('分类名称被占用,请更换!')
        }
        else if (result.length === 1 && req.body.alias === result[0].alias) {
            return res.cc('分类别名被占用,请更换!')
        }
        else {
            //将新分类添加进去
            const addSql = 'insert into ev_artide_cate set ?'
            db.query(addSql, req.body, (err, result) => {
                if (err) return res.cc(err)
                if (result.affectedRows !== 1) return res.cc('新增文章分类失败!')
                res.cc('新增文章分类成功!')
            })

        }

    })
}

exports.deleteCateById = (req, res) => {
    const sql = `delete from ev_artide_cate where id =?`
    db.query(sql, req.query.id, (err, result) => {
        if (err) return res.cc(err)
        if (result.affectedRows !== 1) return res.cc('删除文章分类失败!')
        res.cc('删除文章分类成功!')
    })
}

//根据id查看文章分类
exports.getArtcateShowId = (req, res) => {
    const sql = `select * from ev_artide_cate where id=?`
    db.query(sql, req.params.id, (err, result) => {
        if (err) return res.cc(err)
        if (result.length !== 1) return res.cc('查看文章分类失败!')
        res.cc({
            status: 0,
            data: result[0]
        })
    })

}

exports.updateCateById = (req, res) => {
    //表里面查询重复的sql语句
    const sql = `select * from ev_artide_cate where id<>? and (name =? or alias=?) `
    db.query(sql, [req.body.id, req.body.name, req.body.alias], (err, result) => {
        if (err) return res.cc(err)
        if (result.length === 2) return res.cc('分类名称与别名被占用,请更换')
        if (result.length === 1 && req.body.name === result[0].name && req.body.alias === result[0].alias) {
            return res.cc('分类名称与分类别名被占用,请更换!')
        }
        else if (result.length === 1 && req.body.name === result[0].name) {
            return res.cc('分类名称被占用,请更换!')
        }
        else if (result.length === 1 && req.body.alias === result[0].alias) {
            return res.cc('分类别名被占用,请更换!')
        }
        //更新文章的分类sql
        const updateSql = `update ev_artide_cate set ? where id =?`
        db.query(updateSql, [req.body, req.body.id], (err, result) => {
            if (err) return res.cc(err)
            if (result.affectedRows !== 1) return res.cc('更新失败,id可能不存在')
            res.send('更新成功')
        })

    })
}